//
//  MapImageViewController.m
//  DemoMeshtiles
//
//  Created by MAC on 7/20/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MapImageViewController.h"
#import "ImageAnnotaionView.h"
#import "ImageAnnotation.h"
#define METERS_PER_MILE 1609.344
@interface MapImageViewController ()

@end

@implementation MapImageViewController
@synthesize mapView=_mapView;
@synthesize toolBar;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated {

    [_mapView setDelegate:self];
    [_mapView removeAnnotations:_mapView.annotations];
    //parse jSON
    double sumLatitude=0;
    double sumLongitude=0;
    int size=[photoStreams count];
    int count=0;
    double minLatitude,maxLatitude;
    double minLongitude,maxLongitude;
    BOOL isTheFirst=NO;
    for (int i=0;i<size;i++) {
        ImageAnnotation *imageAnnotaion=[[ImageAnnotation alloc] init];
        NSDictionary *dict=[photoStreams objectAtIndex:i];
        CLLocationCoordinate2D annotaionLocation;
        
        double locationLatitude=[[dict objectForKey:@"latitude"] doubleValue];
        double locationLongitude=[[dict objectForKey:@"longitude"] doubleValue];
        NSLog(@"\n====== Latidute: %f, Longitute: %f",locationLatitude,locationLongitude);
        if ((locationLatitude<180)&&(locationLongitude<180)) {
            
            count++;
            if (isTheFirst==NO) {
                minLatitude=locationLatitude;
                maxLatitude=locationLatitude;
                minLongitude=locationLongitude;
                maxLongitude=locationLongitude;
                isTheFirst=YES;
            }
            if (locationLatitude<minLatitude) minLatitude=locationLatitude;
            if (locationLatitude>maxLatitude) maxLatitude=locationLatitude;
            
            if (locationLongitude<minLongitude) minLongitude=locationLongitude;
            if (locationLongitude>maxLongitude) maxLongitude=locationLongitude;
            
            sumLatitude+=locationLatitude;
            sumLongitude+=locationLongitude;
            //setValue
            annotaionLocation.latitude=locationLatitude;
            annotaionLocation.longitude=locationLongitude;
            imageAnnotaion.coordinate=annotaionLocation;
            imageAnnotaion.imageId=i;
            imageAnnotaion.imageURL=[dict objectForKey:@"url_thumb"];         
            [self.mapView addAnnotation:imageAnnotaion];
            [imageAnnotaion release];
        }
        
    }
    
    //set view region
    // 1
    CLLocationCoordinate2D centerLocation;
    centerLocation.latitude = (maxLatitude-minLatitude)/2;
    centerLocation.longitude= (maxLongitude-minLongitude)/2;
    
    MKCoordinateRegion viewRegion=MKCoordinateRegionMake(centerLocation, MKCoordinateSpanMake((-minLatitude+maxLatitude)/1, (-minLongitude+maxLongitude)/1));
    NSLog(@"Map view did load ?");
    // 4
    MKCoordinateRegion mapViewRegion=[_mapView regionThatFits:viewRegion];  
    [_mapView setRegion:mapViewRegion animated:YES]; 

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark Map View Delegate methods
-(MKAnnotationView*) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    static NSString *const kAnnotationReuseIdentifier = @"CPAnnotationView";
    
    ImageAnnotaionView *annotationView =(ImageAnnotaionView*) [mapView dequeueReusableAnnotationViewWithIdentifier:kAnnotationReuseIdentifier];
    if (annotationView == nil) {
        annotationView = [[[ImageAnnotaionView alloc] initWithAnnotation:annotation reuseIdentifier:kAnnotationReuseIdentifier] autorelease];
        
    }    
    ImageAnnotation *imageAnnotation=(ImageAnnotation*)annotation;
    [annotationView setImageURL:imageAnnotation.imageURL];
    return annotationView;	
    
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
    NSLog(@"Did select anno view %@",view);
    
}
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view {
    NSLog(@"Did deselect anno view %@",view);
}

- (IBAction)buttonClick:(id)sender {
    CLLocationCoordinate2D centerLocation;
    centerLocation=_mapView.region.center;
    MKCoordinateSpan span=MKCoordinateSpanMake(_mapView.region.span.latitudeDelta+2, _mapView.region.span.longitudeDelta+2);
    MKCoordinateRegion viewRegion=MKCoordinateRegionMake(centerLocation, span);   
    // 4
    MKCoordinateRegion mapViewRegion=[_mapView regionThatFits:viewRegion];  
    [_mapView setRegion:mapViewRegion animated:YES]; 
}
@end
